package com.ruoyi.project.orderSystem.service.impl;

import java.util.*;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.seqGen.SeqGenTool;
import com.ruoyi.project.orderSystem.domain.TaskStation;
import com.ruoyi.project.orderSystem.mapper.TaskStationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.orderSystem.mapper.WorkTaskMapper;
import com.ruoyi.project.orderSystem.domain.WorkTask;
import com.ruoyi.project.orderSystem.service.IWorkTaskService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工单任务Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-01-25
 */
@Service
public class WorkTaskServiceImpl implements IWorkTaskService 
{
    @Autowired
    private WorkTaskMapper workTaskMapper;
    @Autowired
    private TaskStationMapper taskStationMapper;

    /**
     * 查询工单任务
     * 
     * @param id 工单任务ID
     * @return 工单任务
     */
    @Override
    public WorkTask selectWorkTaskById(Long id)
    {
        return workTaskMapper.selectWorkTaskById(id);
    }

    /**
     * 查询工单任务列表
     * 
     * @param workTask 工单任务
     * @return 工单任务
     */
    @Override
    public List<WorkTask> selectWorkTaskList(WorkTask workTask)
    {
        return workTaskMapper.selectWorkTaskList(workTask);
    }

    /**
     * 新增工单任务
     * 
     * @param workTask 工单任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWorkTask(WorkTask workTask)
    {
        if(workTask.getCycleNum()==null||workTask.getCycleNum()==0){
            throw new BaseException("请选择周期间隔时间");
        }
        if(workTask.getStationIds()==null||workTask.getStationIds().length==0){
            throw new BaseException("请选择站点");
        }
        Optional.ofNullable(workTask.getTaskCycle()).orElseThrow(()->new BaseException("请选择任务周期"));
        Optional.ofNullable(workTask.getGmtStart()).orElseThrow(()->new BaseException("请选择开始时间"));
//        Optional.ofNullable(workTask.getGmtEnd()).orElseThrow(()->new BaseException("请选择结束时间"));
        Optional.ofNullable(workTask.getExecutorIds()).orElseThrow(()->new BaseException("请选择任务执行人"));
        try {
            workTask.setTaskSn("WT"+ SeqGenTool.seq());
            Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
            workTask.setCreator(userId);
            workTask.setGmtCreate(new Date());

            int i = workTaskMapper.insertWorkTask(workTask);
            if(i>0){
                List<TaskStation> taskStations = new ArrayList<>();
                Arrays.stream(workTask.getStationIds()).forEach(x->{
                TaskStation taskStation = new TaskStation();
                    taskStation.setStationId(x);
                    taskStation.setTaskId(workTask.getId());
                    taskStations.add(taskStation);
                });
                if(taskStations.size()>0){
                    taskStationMapper.batchInsert(taskStations);
                }
            }
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("新增任务失败");
        }
    }

    /**
     * 修改工单任务
     * 
     * @param workTask 工单任务
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWorkTask(WorkTask workTask)
    {
        try {
            Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
            workTask.setModifier(userId);
            workTask.setGmtModify(new Date());
            if(workTask.getStationIds()!=null&&workTask.getStationIds().length>0){
                taskStationMapper.deleteTaskStationByTaskId(workTask.getId());
                List<TaskStation> taskStations = new ArrayList<>();
                Arrays.stream(workTask.getStationIds()).forEach(x->{
                    TaskStation taskStation = new TaskStation();
                    taskStation.setStationId(x);
                    taskStation.setTaskId(workTask.getId());
                    taskStations.add(taskStation);
                });
                if(taskStations.size()>0){
                    taskStationMapper.batchInsert(taskStations);
                }
            }
            return workTaskMapper.updateWorkTask(workTask);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException("工单任务修改失败");
        }
    }

    /**
     * 批量删除工单任务
     * 
     * @param ids 需要删除的工单任务ID
     * @return 结果
     */
    @Override
    public int deleteWorkTaskByIds(Long[] ids)
    {
        return workTaskMapper.deleteWorkTaskByIds(ids);
    }

    /**
     * 删除工单任务信息
     * 
     * @param id 工单任务ID
     * @return 结果
     */
    @Override
    public int deleteWorkTaskById(Long id)
    {
        return workTaskMapper.deleteWorkTaskById(id);
    }

    @Override
    public List<WorkTask> getWorkTaskListOntime() {
        return workTaskMapper.getWorkTaskListOntime();
    }
}
